/*
 * Copyright (c) Created by T C L on 2019.7.9.
 */

package cn.mumzone.pos.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunmi.payment.PaymentService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.RxUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.BuildConfig;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.event.TimesCardReturnRealMoneyEvent;
import cn.mumzone.pos.model.CardReturnEntity;
import cn.mumzone.pos.model.ReturnEntity;
import cn.mumzone.pos.model.SunMiPayRequestEntity;
import cn.mumzone.pos.model.VerifyReturnAuthEntity;
import cn.mumzone.pos.model.source.AppRepository;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.model.source.UserRepository;
import cn.mumzone.pos.presenter.contract.TimesCardReturnContract;
import cn.mumzone.pos.util.BillUtil;
import cn.mumzone.pos.util.PayCountDownTimer;
import retrofit2.Response;
import rx.Subscription;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

public class TimesCardReturnPresenter implements TimesCardReturnContract.Presenter {


    private TimesCardReturnContract.View view;
    private Context context;
    private CompositeSubscription compositeSubscription;
    private MemberRepository memberRepository;
    private UserRepository userRepository;


    public TimesCardReturnPresenter(Context context, final TimesCardReturnContract.View view) {
        this.context = context;
        this.view = view;
        compositeSubscription = new CompositeSubscription();
        memberRepository = new MemberRepository();
        userRepository = new UserRepository();
    }


    @Override
    public void requestReturnCardDetail(String id, String times, String residueTimes, String goodsId,
                                        String orderCode) {
        memberRepository.getReturnCardDetail(id, times, residueTimes, goodsId, orderCode,
                new ApiCallBack<CardReturnEntity>() {
                    @Override
                    public void onSuccess(CardReturnEntity cardReturnEntity) {
                        view.setReturnCardDetailData(cardReturnEntity);
                    }

                    @Override
                    public void onError(BaseResponse response) {
                        ToastUtil.makeText(context, response.getMsg(), false);
                    }
                });
    }

    @Override
    public void goReturnCard(String id, String price, final String residuePrice, String goodsId, String orderCode,
                             String returnMargin) {
        ApiCallBack<List<ReturnEntity>> callBack = new ApiCallBack<List<ReturnEntity>>() {
            @Override
            public void onSuccess(List<ReturnEntity> returnEntities) {
                view.onReturnSuccess(returnEntities);
            }

            @Override
            public void onError(BaseResponse response) {
                view.onReturnFailed(response.getMsg());
            }
        };
        memberRepository.returnCard(id, price, residuePrice, goodsId, orderCode, returnMargin, callBack);
        compositeSubscription.add(callBack);
    }




    public void verifyReturnAuth() {
        ApiCallBack<VerifyReturnAuthEntity> callBack = new ApiCallBack<VerifyReturnAuthEntity>() {
            @Override
            public void onSuccess(VerifyReturnAuthEntity verifyReturnAuthEntity) {
                if (!StringUtils.isEmpty(verifyReturnAuthEntity.getAccessType()) && verifyReturnAuthEntity.getAccessType().equals("1")) {
                    view.showUpdateReturnAmount();
                } else {
                    view.goAuthView();
                }
            }

            @Override
            public void onError(BaseResponse response) {
                PosToast.INSTANCE.makeShortToast(context, response.getMsg());
            }
        };
        userRepository.verifyReturnAuth(AppConstants.TEMP_SELLER_ID, "", "", callBack);
        compositeSubscription.add(callBack);
    }



    @Override
    public void initEvent() {
        //回调实退金额
        Subscription subscription = RxBus.getDefault()
                .toObservable(TimesCardReturnRealMoneyEvent.class)
                .subscribe(new Action1<TimesCardReturnRealMoneyEvent>() {
                    @Override
                    public void call(TimesCardReturnRealMoneyEvent timesCardReturnRealMoneyEvent) {
                        view.setRealReturnMoney(timesCardReturnRealMoneyEvent.getRealMoney());
                    }
                });
        compositeSubscription.add(subscription);
    }

    @Override
    public void destroy() {
        if (compositeSubscription != null) {
            compositeSubscription.unsubscribe();
        }
    }

}
